{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装pypinyin模块\n",
    "该模块的官方文档：<br>\n",
    "* [中文文档](https://pypinyin.readthedocs.io/zh_CN/master/index.html)\n",
    "* [pypinyin模块相关博客](https://blog.csdn.net/qq_42992919/article/details/98485906)<br>\n",
    "**方法一(推荐！！！)**：搜索Anaconda prompt下载第三方模块pypinyin<br>\n",
    "![Anaconda prompt](ana_prompt.png)<br>\n",
    "输入以下代码进行安装：**pip install pypinyin -i http://pypi.douban.com/simple --trusted-host pypi.douban.com**<br>\n",
    "**方法二**：打开命令指示符【输入cmd打开】安装pypinyin模块<br>\n",
    "输入以下代码进行安装：**pip install pypinyin -i http://pypi.douban.com/simple --trusted-host pypi.douban.com**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypinyin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Python语法重要知识点补充【列表->字符串；字符串->列表】"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 字符串转列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "#### list()方法:不可指定分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'w', 'o', 'r', 'r', 'y']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "saying=\"Don't worry\"\n",
    "saying_list=list(saying)\n",
    "saying_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "#### split()方法：可指定分隔符；不填则默认一个' '【空格】作为划分依据 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"Don't\", 'worry']\n",
      "['Don', 't worry']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'t worry'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "saying=\"Don't worry\"\n",
    "saying_split=saying.split()\n",
    "print(saying_split)\n",
    "saying_split_s=saying.split(\"'\")\n",
    "print(saying_split_s)\n",
    "saying_split_s[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 列表转字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "#### \"\".join():引号中间是作为分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Don't worry\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "saying=['D', 'o', 'n', \"'\", 't', ' ', 'w', 'o', 'r', 'r', 'y']\n",
    "list_str=\"\".join(saying)\n",
    "list_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"D o n ' t   w o r r y\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#引号中间添加一个空格\n",
    "saying=['D', 'o', 'n', \"'\", 't', ' ', 'w', 'o', 'r', 'r', 'y']\n",
    "list_str=\" \".join(saying)\n",
    "list_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(type(list_str))\n",
    "print(type(saying))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 批量将表格中的全部中文姓名转为拼音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>姓名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>刘泽燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>黄小珊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>潘学杰</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>冯雅雯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>刘奕璇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>124</td>\n",
       "      <td>刘琪涛</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <td>125</td>\n",
       "      <td>唐子君</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>126</td>\n",
       "      <td>汤晓骅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>126</th>\n",
       "      <td>127</td>\n",
       "      <td>刘欣然</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>127</th>\n",
       "      <td>128</td>\n",
       "      <td>陈晓丽</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>128 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      序号   姓名\n",
       "0      1  刘泽燕\n",
       "1      2  黄小珊\n",
       "2      3  潘学杰\n",
       "3      4  冯雅雯\n",
       "4      5  刘奕璇\n",
       "..   ...  ...\n",
       "123  124  刘琪涛\n",
       "124  125  唐子君\n",
       "125  126  汤晓骅\n",
       "126  127  刘欣然\n",
       "127  128  陈晓丽\n",
       "\n",
       "[128 rows x 2 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df=pd.read_excel('C://Users//1//代码//2020励能计划-讲义//week03-数据结构：字典、购物车、pandas列转表、表转列/课程名单.xlsx')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 取出前十先看看是怎么的思路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>姓名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>刘泽燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>黄小珊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>潘学杰</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>冯雅雯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>刘奕璇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>燕东芝</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>徐硕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>毕唯玮</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>招泳淇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>何易莼</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   序号   姓名\n",
       "0   1  刘泽燕\n",
       "1   2  黄小珊\n",
       "2   3  潘学杰\n",
       "3   4  冯雅雯\n",
       "4   5  刘奕璇\n",
       "5   6  燕东芝\n",
       "6   7   徐硕\n",
       "7   8  毕唯玮\n",
       "8   9  招泳淇\n",
       "9  10  何易莼"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_前十=df.head(10)#pandas方法.head()括号中可填数字n表示取出表格前n行，不填默认取出表格前五行\n",
    "df_前十"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "返回名字转为拼音结果 [['liu'], ['ze'], ['yan']]\n",
      "拼音列表 ['liu', 'ze', 'yan']\n",
      "列表转字符串 Liu Zeyan\n",
      "只取首字母 LZY\n",
      "返回名字转为拼音结果 [['huang'], ['xiao'], ['shan']]\n",
      "拼音列表 ['huang', 'xiao', 'shan']\n",
      "列表转字符串 Huang Xiaoshan\n",
      "只取首字母 HXS\n",
      "返回名字转为拼音结果 [['pan'], ['xue'], ['jie']]\n",
      "拼音列表 ['pan', 'xue', 'jie']\n",
      "列表转字符串 Pan Xuejie\n",
      "只取首字母 PXJ\n",
      "返回名字转为拼音结果 [['feng'], ['ya'], ['wen']]\n",
      "拼音列表 ['feng', 'ya', 'wen']\n",
      "列表转字符串 Feng Yawen\n",
      "只取首字母 FYW\n",
      "返回名字转为拼音结果 [['liu'], ['yi'], ['xuan']]\n",
      "拼音列表 ['liu', 'yi', 'xuan']\n",
      "列表转字符串 Liu Yixuan\n",
      "只取首字母 LYX\n",
      "返回名字转为拼音结果 [['yan'], ['dong'], ['zhi']]\n",
      "拼音列表 ['yan', 'dong', 'zhi']\n",
      "列表转字符串 Yan Dongzhi\n",
      "只取首字母 YDZ\n",
      "返回名字转为拼音结果 [['xu'], ['shuo']]\n",
      "拼音列表 ['xu', 'shuo']\n",
      "列表转字符串 Xu Shuo\n",
      "只取首字母 XS\n",
      "返回名字转为拼音结果 [['bi'], ['wei'], ['wei']]\n",
      "拼音列表 ['bi', 'wei', 'wei']\n",
      "列表转字符串 Bi Weiwei\n",
      "只取首字母 BWW\n",
      "返回名字转为拼音结果 [['zhao'], ['yong'], ['qi']]\n",
      "拼音列表 ['zhao', 'yong', 'qi']\n",
      "列表转字符串 Zhao Yongqi\n",
      "只取首字母 ZYQ\n",
      "返回名字转为拼音结果 [['he'], ['yi'], ['chun']]\n",
      "拼音列表 ['he', 'yi', 'chun']\n",
      "列表转字符串 He Yichun\n",
      "只取首字母 HYC\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\1\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:31: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "C:\\Users\\1\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:32: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>英文名</th>\n",
       "      <th>拼音首字母</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>刘泽燕</td>\n",
       "      <td>Liu Zeyan</td>\n",
       "      <td>LZY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>黄小珊</td>\n",
       "      <td>Huang Xiaoshan</td>\n",
       "      <td>HXS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>潘学杰</td>\n",
       "      <td>Pan Xuejie</td>\n",
       "      <td>PXJ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>冯雅雯</td>\n",
       "      <td>Feng Yawen</td>\n",
       "      <td>FYW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>刘奕璇</td>\n",
       "      <td>Liu Yixuan</td>\n",
       "      <td>LYX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>燕东芝</td>\n",
       "      <td>Yan Dongzhi</td>\n",
       "      <td>YDZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>徐硕</td>\n",
       "      <td>Xu Shuo</td>\n",
       "      <td>XS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>毕唯玮</td>\n",
       "      <td>Bi Weiwei</td>\n",
       "      <td>BWW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>招泳淇</td>\n",
       "      <td>Zhao Yongqi</td>\n",
       "      <td>ZYQ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>何易莼</td>\n",
       "      <td>He Yichun</td>\n",
       "      <td>HYC</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   序号   姓名             英文名 拼音首字母\n",
       "0   1  刘泽燕       Liu Zeyan   LZY\n",
       "1   2  黄小珊  Huang Xiaoshan   HXS\n",
       "2   3  潘学杰      Pan Xuejie   PXJ\n",
       "3   4  冯雅雯      Feng Yawen   FYW\n",
       "4   5  刘奕璇      Liu Yixuan   LYX\n",
       "5   6  燕东芝     Yan Dongzhi   YDZ\n",
       "6   7   徐硕         Xu Shuo    XS\n",
       "7   8  毕唯玮       Bi Weiwei   BWW\n",
       "8   9  招泳淇     Zhao Yongqi   ZYQ\n",
       "9  10  何易莼       He Yichun   HYC"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#新建两个列表\n",
    "pinyin_name = []\n",
    "first_pinyin = []\n",
    "#for循环将每个名字循环遍历出来\n",
    "for i in df_前十['姓名']:\n",
    "    #style参数指定的是拼音的风格；\n",
    "    #NORMAL是普通风格，不带声调\n",
    "    #TONE是标准声调风格，拼音声调在韵母第一个字母上（默认风格）\n",
    "    result = pypinyin.pinyin(i,style=pypinyin.NORMAL)#返回每个中文字符对应拼音的嵌套列表\n",
    "    print('返回名字转为拼音结果',result)\n",
    "    \n",
    "    result_ = [i[0] for i in result]#列表推导式：取出每一个拼音并添加到一个列表中去\n",
    "    print('拼音列表',result_)\n",
    "    \n",
    "    #.capitalize()将字符串的第一个字母变成大写,其他字母变小写\n",
    "    #result_[1:]从result_列表中的索引1【切记切记：索引值从0开始】取值到列表末尾【冒号右边“：”不填代表取值到末尾】\n",
    "    #''.join()将列表转为字符串，最后再用.capitalize()将字符串的第一个字母变成大写,其他字母变小写\n",
    "    result_拼音 = result_[0].capitalize() + ' ' + ''.join(result_[1:]).capitalize()\n",
    "    print('列表转字符串',result_拼音)\n",
    "    \n",
    "    #列表推导式:将每个字的拼音首字母取出来并用.upper()Python内置方法将字符转为大写\n",
    "    #转大写后存到新的列表中并用''.join()方法转为字符串\n",
    "    result_缩写 = ''.join([i[0].upper() for i in result_])\n",
    "    print('只取首字母',result_缩写)\n",
    "    \n",
    "    #将拼音及拼音缩写添加到新的列表中去\n",
    "    pinyin_name.append(result_拼音)\n",
    "    first_pinyin.append(result_缩写)\n",
    "\n",
    "#将拼音及拼音缩写添加到原表格中\n",
    "df_前十['英文名'] = pinyin_name\n",
    "df_前十['拼音首字母'] = first_pinyin\n",
    "df_前十\n",
    "# df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_前十.to_excel('1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
